package system1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type WorkflowOwnerApi struct {
}

var WorkflowOwnerService = service.ServiceGroupApp.System1ServiceGroup.WorkflowOwnerService

// CreateWorkflowOwner 创建当前审批处理人
// @Tags WorkflowOwner
// @Summary 创建当前审批处理人
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowOwner true "创建当前审批处理人"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /WorkflowOwner/createWorkflowOwner [post]
func (WorkflowOwnerApi *WorkflowOwnerApi) CreateWorkflowOwner(c *gin.Context) {
	var WorkflowOwner system1.WorkflowOwner
	err := c.ShouldBindJSON(&WorkflowOwner)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowOwner.CreatedBy = utils.GetUserID(c)

	if err := WorkflowOwnerService.CreateWorkflowOwner(&WorkflowOwner); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteWorkflowOwner 删除当前审批处理人
// @Tags WorkflowOwner
// @Summary 删除当前审批处理人
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowOwner true "删除当前审批处理人"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /WorkflowOwner/deleteWorkflowOwner [delete]
func (WorkflowOwnerApi *WorkflowOwnerApi) DeleteWorkflowOwner(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := WorkflowOwnerService.DeleteWorkflowOwner(ID, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteWorkflowOwnerByIds 批量删除当前审批处理人
// @Tags WorkflowOwner
// @Summary 批量删除当前审批处理人
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /WorkflowOwner/deleteWorkflowOwnerByIds [delete]
func (WorkflowOwnerApi *WorkflowOwnerApi) DeleteWorkflowOwnerByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	if err := WorkflowOwnerService.DeleteWorkflowOwnerByIds(IDs, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateWorkflowOwner 更新当前审批处理人
// @Tags WorkflowOwner
// @Summary 更新当前审批处理人
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.WorkflowOwner true "更新当前审批处理人"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /WorkflowOwner/updateWorkflowOwner [put]
func (WorkflowOwnerApi *WorkflowOwnerApi) UpdateWorkflowOwner(c *gin.Context) {
	var WorkflowOwner system1.WorkflowOwner
	err := c.ShouldBindJSON(&WorkflowOwner)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	WorkflowOwner.UpdatedBy = utils.GetUserID(c)

	if err := WorkflowOwnerService.UpdateWorkflowOwner(WorkflowOwner); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindWorkflowOwner 用id查询当前审批处理人
// @Tags WorkflowOwner
// @Summary 用id查询当前审批处理人
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.WorkflowOwner true "用id查询当前审批处理人"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /WorkflowOwner/findWorkflowOwner [get]
func (WorkflowOwnerApi *WorkflowOwnerApi) FindWorkflowOwner(c *gin.Context) {
	ID := c.Query("ID")
	if reWorkflowOwner, err := WorkflowOwnerService.GetWorkflowOwner(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reWorkflowOwner": reWorkflowOwner}, c)
	}
}

// GetWorkflowOwnerList 分页获取当前审批处理人列表
// @Tags WorkflowOwner
// @Summary 分页获取当前审批处理人列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1Req.WorkflowOwnerSearch true "分页获取当前审批处理人列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /WorkflowOwner/getWorkflowOwnerList [get]
func (WorkflowOwnerApi *WorkflowOwnerApi) GetWorkflowOwnerList(c *gin.Context) {
	var pageInfo system1Req.WorkflowOwnerSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := WorkflowOwnerService.GetWorkflowOwnerInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
